﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using ShootSearch.FileSystem;
using ShootSearch.Core;
using log4net;

namespace ShootSearch.Parsers
{
    public class HtmlParser : IParser
    {
        public HtmlParser()
        {
            ContentType = new List<string>();

            ContentType.Add("text/html");
            ContentType.Add("text/xhtml");

        }

        #region IFilter Members

        public List<string> ContentType { get; protected set; }
        public bool Parse( IFileObject input, out IFileObject output)
        {            
            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

            #region Load Html
            try
            {
                htmlDoc.LoadHtml(input.Encoding.GetString(input.Content));

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }

            if (htmlDoc == null || htmlDoc.DocumentNode == null)
            {
                output = null;
                return false;
            }
            #endregion

            FileObjectText doc = new FileObjectText(input.Url, input.DateTime);

            //Get title
            HtmlAgilityPack.HtmlNode titleNode = htmlDoc.DocumentNode.SelectSingleNode("/html/head/title");
            if (titleNode != null)
            {
                doc.Add("Title", titleNode.InnerText);
            }
            else
            {
                doc.Add("Title", input.Url);
            }
            //Get content
            doc.Add("Content", htmlDoc.DocumentNode.InnerText);

            output = doc;
            return true;

        }


        #endregion

        #region IConfigable Members

        public ShootSearch.Core.SpiderManConfig Config { get; set; }
        public ILog Logger { get; set; }

        #endregion


    }
}
